package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.dto.cust.ContextCustDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class CustInfoLocationHandler extends AbstractAttrHandler {

    @Override
    public void init(CommonAttrVO commonAttr) {
    	ContextCustDTO cust_DTO = KiteWebContext.getContext().getCust();
        CuccContextCustDTO custDTO = KiteBeanUtils.transform(cust_DTO, CuccContextCustDTO.class);
        if(KiteStringUtils.isEmpty(custDTO.getLocation())){
            List<String> regionCodes = new ArrayList<>();
            if (KiteStringUtils.isNotEmpty(custDTO.getProvinceCode())) {
                regionCodes.add(custDTO.getProvinceCode());
            }
            if (KiteStringUtils.isNotEmpty(custDTO.getEparchyCode())) {
                regionCodes.add(custDTO.getEparchyCode());
            }
            if (KiteStringUtils.isNotEmpty(custDTO.getRegionId())) {
                regionCodes.add(custDTO.getRegionId());
            }
            commonAttr.setValue(KiteStringUtils.join(regionCodes, SymbolConsts.COMMA));
        }else{
            commonAttr.setValue(custDTO.getLocation());
        }
        List<String> valueList = new ArrayList<String>();
        valueList.add(custDTO.getProvinceCode());
        valueList.add(custDTO.getEparchyCode());
        valueList.add(custDTO.getRegionId());
        valueList.removeAll(Arrays.asList("",null));
        String valueDesc = LocalAttrHelper.getRegionNames(valueList, SymbolConsts.HYPHEN);
        commonAttr.setValueDesc(valueDesc);
        setCommonAttr(commonAttr);
    }
}
